package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.*;
import com.example.elderlycare.dao.mapper.*;
import com.example.elderlycare.service.PerformanceService;
import com.example.elderlycare.service.entity.PerformanceItem;
import com.example.elderlycare.service.entity.QueryListResponseBody;
import com.example.elderlycare.utils.FormatDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PerformanceImpl extends ServiceImpl<PerformanceMapper, Performance> implements PerformanceService {
    @Resource
    PerformanceMapper performanceMapper;

    @Resource
    DepartmentMapper departmentMapper;

    @Resource
    CommonMapper commonMapper;

    @Resource
    EmployeeMapper employeeMapper;

    @Resource
    NursingHomeMapper nursingHomeMapper;

    @Override
    public QueryListResponseBody<PerformanceItem> queryListResponseBody(Map<String, Object> params) {
        int count = performanceMapper.queryPerformanceCount(params);
        List<Performance> performanceList = performanceMapper.queryPerformanceList(params);
        List<PerformanceItem> performanceItemList = performanceList.stream().map(item -> {
            PerformanceItem.PerformanceItemBuilder performanceItemBuilder = PerformanceItem.builder();

            Employee employee = employeeMapper.selectById(item.getEmployeeId());
            performanceItemBuilder.employeeId(item.getEmployeeId()).
                    employee(employee == null ? "未定义" : employee.getName());

            if (employee == null) {
                performanceItemBuilder.nurseHomeId(0).
                        department("未定义").
                        departmentId(0).
                        nurseHome("未定义").
                        positionId(0).
                        position("未定义");
            }
            if (employee != null) {
                NursingHome nursingHome = nursingHomeMapper.selectById(employee.getNurseId());
                performanceItemBuilder.nurseHomeId(employee.getNurseId()).
                        nurseHome(nursingHome == null ? "未定义" : nursingHome.getName());

                Common position = commonMapper.selectById(employee.getPositionId());
                performanceItemBuilder.positionId(employee.getPositionId()).
                        position(position == null ? "未定义" : position.getName());
                Department department = departmentMapper.selectById(employee.getDepartmentId());
                performanceItemBuilder.departmentId(employee.getDepartmentId()).
                        department(department == null ? "未定义" : department.getName());
            }

            performanceItemBuilder.id(item.getId()).
                    countRoster(item.getCountRoster()).
                    countLeave(item.getCountLeave()).
                    countAbsent(item.getCountAbsent()).
                    originPer(item.getOriginPer()).
                    nowPer(item.getNowPer()).
                    timeDate(FormatDate.getFormatDateMonth(item.getTimeDate())).
                    rosterBase(item.getRosterBase()).
                    leaveBase(item.getLeaveBase()).
                    absentBase(item.getAbsentBase()).
                    award(item.getAward()).
                    punish(item.getPunish());

            return performanceItemBuilder.build();
        }).collect(Collectors.toList());

        return QueryListResponseBody.<PerformanceItem>builder().
                total(count).
                items(performanceItemList).
                build();
    }
}
